<?php
/**
 * @author wangwei
 * 2015-11-24
 * UTF-8
 */
class Model_Uaction_UactionRecord extends Model_BaseModel {
    /**
     * 缓存前缀
     *
     * @var
     */
    private $table = 'user_action_records';
    private static $tag = 'Model_Uaction_UactionRecord_Tag_';
    private static $expire = 3600;

    function __construct(){
        // 选择连接的数据库
        parent::_init ( 'mediation' );
    }


    /**
     * 添加
     * @param array $saveData
     * @param int $pk
     */
    function addData($saveData)
    {
        $arrFields = $arrValues = $data = array ();
        foreach ( $saveData as $key => $value ) {
            $arrFields [] = '`' . $key . '`';
            $temp = ':' . $key;
            $data [$temp] = $value;
            $arrValues [] = $temp;
        }
        $sql = 'INSERT INTO `' . $this->table . '` (' . implode ( ',', $arrFields ) . ') VALUES (' . implode ( ',', $arrValues ) . ')';
        $res = $this->dao->conn ( false )->setTag ( self::$tag )->preparedSql ( $sql, $data )->lastInsertId ();
        return $res;
    }

    /**
     * @param array $fields
     * @param array $search
     * @param bool $cache
     * @return mixed
     */
    public function getAllBySearch($fields = array(),$search = array(),$cache = false){
        $fields = empty($fields) ? '*' : implode(',',$fields);
        $search_str = '';
        if(isset($search['where']) && !empty($search['where'])){
            $search_str .= 'WHERE '.implode(' AND ',$search['where']);
        }
        if(isset($search['order']) && $search['order']){
            $search_str .= ' ORDER BY '.$search['order'];
        }
        if(isset($search['limit']) && $search['limit']){
            $search['start'] = isset($search['start']) ? $search['start'] : 0;
            $search_str .= ' LIMIT '.$search['start'].','.$search['limit'];
        }
        $sql = "SELECT {$fields} FROM {$this->table} {$search_str}";
        $cache ? $this->dao->conn()->setTag(self::$tag) : $this->dao->conn()->noCache();
        $res = $this->dao->preparedSql($sql, array())->fetchAll();
        return $res;
    }

}